const app = getApp();
var unbind_id = '';
const pageSize = 20;

const cardTypes = [{
  value: "",
  name: "全部"
}, {
  value: "2",
  name: "普通卡"
}, {
  value: "1",
  name: "特殊卡"
}];
const cardStatuses = [{
  value: "",
  name: "全部"
}, {
  value: "1",
  name: "正常"
}, {
  value: "2",
  name: "挂失"
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    salesman_id: '',
    total: 0,
    modelshow: true,
    // 1解绑 2挂失 3解挂
    modelstate: '',
    card_data: [],
    page: 1,
    isFilter: false,
    drawer: false,
    menuIndex: "", //展开更多菜单的列表项
    loadmore: false,
    nomore: false,
    conditions: { //存放搜索条件
      user_keyword: "",
      start_money: "",
      end_money: "",
      cardTypeIndex: "",
      cardStatusIndex: "",
    },
    virtual_card: '',
    entity_card: '',
    cardTypes,
    cardStatuses,
    empty: false,
    cardIndex: '', //操作数据的序号
    isModalClear: false, //置0弹窗
    modalClearContent: "", //置0弹窗文字
    balance: "",
    isModalInput: false,
    showInput: false,
    franchisees: false, //区别是否为加盟商
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      salesman_id: options.id
    })
    this.waterCardList();
    this.getCarType();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.waterCardList();
    }
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  reloadData() {
    this.setData({
      page: 1,
      card_data: [],
      nomore: false,
      loadmore: false,
      total: 0
    })
    wx.showLoading();
    this.waterCardList();
  },



  onShow: function () {
    unbind_id = '';
  },


  //获取卡类型
  getCarType() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getCarType',
      method: "POST",
      data: {
        is_search: 1
      },
      success: function (res) {
        var flag = res.data;
        if (flag.code == 1000) {
          that.setData({
            typeList: flag.data
          });

        }
      }
    });
  },

  // 解绑
  Untie: function (e) {
    this.setData({
      modelshow: false,
      modelstate: 1,
      entity_card: e.currentTarget.dataset.entity_card,
      // unbind_id: e.currentTarget.dataset.unbind_id,
      virtual_card: e.currentTarget.dataset.virtual_card,
    })
    unbind_id = e.currentTarget.dataset.unbind_id;
  },

  // 挂失
  reloss: function (e) {
    this.setData({
      modelshow: false,
      modelstate: 2,
      entity_card: e.currentTarget.dataset.entity_card,
      // unbind_id: e.currentTarget.dataset.unbind_id,
      virtual_card: e.currentTarget.dataset.virtual_card,
    })
    unbind_id = e.currentTarget.dataset.unbind_id;
  },
  report_loss() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/reportLoss',
      method: "POST",
      data: {
        id: unbind_id //水卡ID
      },
      success: function (res) {
        var flag = res.data;
        app.showToast(flag.message);
        if (flag.code > 0) {
          that.setData({
            modelshow: true
          })
          setTimeout(() => {
            that.reloadData()
          }, 1500)
        }
      }
    });
  },
  remove_loss() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/unReportLoss',
      method: "POST",
      data: {
        id: unbind_id //水卡ID
      },
      success: function (res) {
        var flag = res.data;
        app.showToast(flag.message);
        if (flag.code > 0) {
          that.setData({
            modelshow: true
          })
          setTimeout(() => {
            that.reloadData()
          }, 1500)
        }
      }
    });
  },
  // 解挂
  hangout: function (e) {
    this.setData({
      modelshow: false,
      modelstate: 3,
      entity_card: e.currentTarget.dataset.entity_card,
      // unbind_id: e.currentTarget.dataset.unbind_id,
      virtual_card: e.currentTarget.dataset.virtual_card,
    })
    unbind_id = e.currentTarget.dataset.unbind_id;
  },

  // 关闭弹窗
  closemodel: function () {
    this.setData({
      modelshow: true
    })
  },



  waterCardList() {
    let conditions = this.data.conditions;
    app.ajax({
      url: 'Partner/WaterCard/waterCardList',
      method: "POST",
      data: {
        page: this.data.page,
        row: pageSize,
        salesman_id: this.data.salesman_id,
        user_keyword: conditions.user_keyword,
        start_money: conditions.start_money,
        end_money: conditions.end_money,
        card_status: conditions.cardStatusIndex !== "" ? this.data.cardStatuses[conditions.cardStatusIndex].value : '',
        is_special: conditions.cardTypeIndex !== "" ? this.data.cardTypes[conditions.cardTypeIndex].value : "",
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          const source = res.data.data;
          let list = this.data.card_data.concat(source.data);
          this.setData({
            card_data: list,
            empty: source.page.total_number == 0 ? true : false,
            loadmore: source.page.total_number > list.length ? true : false,
            nomore: source.page.total_number <= list.length ? true : false,
            drawer: false,
            total: source.page.total_number
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },

  handleMore(e) {
    this.setData({
      menuIndex: e.currentTarget.dataset.index
    })
  },

  closeMenu() {
    this.setData({
      menuIndex: ""
    })
  },

  handleInput(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  hideClear() {
    this.setData({
      isModalClear: false
    })
    delete (this.water_id);
    delete (this.selectIndex)
  },
  handleClear(e) {
    const index = e.detail.index;
    if (index == 0) {
      this.hideClear()
    } else {
      if (this.submit) {
        app.showToast("正在处理中...");
        return false
      }
      this.submit = true;
      app.ajax({
        url: "Partner/WaterCard/cardBalanceUpdate",
        data: {
          id: this.water_id,
          type: 2
        },
        success: res => {
          delete (this.submit);
          if (res.data.code == 1000) {
            app.showToast(res.data.message, "success");
            this.setData({
              [`list[${this.selectIndex}].balance`]: "0.000"
            })
          } else {
            app.showToast(res.data.message)
          }
          this.hideClear();
        },
        error: e => {
          delete (this.submit);
          app.showToast("请求失败")
        }
      })
    }
  },
  showModalClear(e) {
    const data = e.currentTarget.dataset;
    this.water_id = data.id;
    this.selectIndex = data.index;
    this.setData({
      isModalClear: true,
      modalClearContent: `确认将${data.name && `用户【${data.name}】`}该水卡清零？`
    })
  },
  alterBalance() {
    if (this.sub == true) {
      app.showToast("正在处理中...");
      return false;
    }
    this.sub = true;
    app.ajax({
      url: "Partner/WaterCard/cardBalanceUpdate",
      data: {
        id: this.water_id,
        to_money: this.data.balance,
        type: 1
      },
      success: res => {
        delete (this.sub);
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          this.setData({
            [`list[${this.alterIndex}].balance`]: parseFloat(this.data.balance).toFixed(3)
          })
        } else {
          app.showToast(res.data.message);
        }
        this.hideModalInput();
      },
      error: e => {
        delete (this.sub)
      }
    })
  },
  showModalInput(e) {
    const data = e.currentTarget.dataset;
    this.water_id = data.id;
    this.alterIndex = data.index;
    this.setData({
      isModalInput: true,
      showInput: true,
      balance: data.balance
    })
  },
  hideModalInput() {
    this.setData({
      isModalInput: false,
      balance: ""
    })
    setTimeout(() => {
      this.setData({
        showInput: false
      })
    }, 100)
    delete (this.water_id);
    delete (this.alterIndex)
  }
})